<?php
/**
 * Created by PhpStorm.
 * User: xuke
 * Date: 16/6/23
 * Time: 下午3:53
 */

namespace App\Logic;
use App\Http\Model\OrderSub AS OrderSubModel;
use App\Exceptions\ApiException;

class OrderSubLogic extends BaseLogic
{
    /**
     * @var $_subOrder OrderSubModel
     */
    protected $_subOrder = null;


    public function __construct($subOrder)
    {
        $this->_subOrder = $subOrder;
    }

    /**
     * @param $subOrder int|OrderSubModel
     * @return static
     * @throws ApiException
     */
    public static function initBySubOrder($subOrder)
    {
        if (is_numeric($subOrder)) {
            $subOrder = OrderSubModel::find($subOrder);
        } else {
            if (!$subOrder instanceof OrderSubModel) {
                throw new ApiException(-2, '子订单参数错误');
            }
        }

        return new static($subOrder);
    }

    public function create()
    {
        $subOrder = new OrderSubModel();

        return new static($subOrder);
    }


    /**设置主订单号
     *
     * @param $order
     */
    public function setOrderId($order)
    {
        $this->getSubOrder()->setOrder($order);

        return $this;
    }

    public function getSubOrder()
    {
        return $this->_subOrder;
    }

    /**
     * @param $provider
     */
    public function setProvider($provider)
    {
        $this->getSubOrder()->setProvider($provider);
        return $this;
    }

    /**
     *
     */
    public function save($data)
    {
        return $this->getSubOrder()->fill($data)->save();
    }

}